<template>
  <div class="app-container">

    <el-button type="primary" @click="handleAddRole">新增</el-button>
    <div style="height:650px">
      <el-table :data="rolesList" v-loading='loading' style="width: 100%;margin-top:30px;" border>

        <el-table-column align="center" label="标题">
          <template slot-scope="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="产品类型">
          <template slot-scope="scope">
            {{ type(scope.row.purchaseId)}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="产品价格">
          <template slot-scope="scope">
            {{ scope.row.payPirce}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="产品链接">
          <template slot-scope="scope">
            {{ scope.row.downUrl}}
          </template>
        </el-table-column>

        <!-- <el-table-column align="center" width="900" label="产品内容">
              <template slot-scope="scope">
                {{ scope.row.content}}
              </template>
            </el-table-column> -->

        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">修改</el-button>
            <el-popconfirm title="是否删除" @confirm='handleDelete(scope.row)'>
              <el-button slot="reference" type="danger">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <!-- 添加对话框 -->
    <el-dialog :visible.sync="createDialogVisible" @close="handleCancel" :close-on-click-modal="false" :title="isAddOrUpdata?'新增详情':'修改详情'">
      <el-form :rules="rules" ref="ruleForm" :model="ruleForm" label-width="150px" v-loading='loading'>

        <el-form-item label="标题：" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>

        <el-form-item label="产品类型：" prop="purchaseId">
          <el-select v-model="ruleForm.purchaseId" placeholder="请选择">
            <el-option v-for="item in purchaseList" :key="item.id" :label="item.title" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="价格：" prop="payPirce">
          <el-input v-model="ruleForm.payPirce"></el-input>
        </el-form-item>
        <el-form-item label="链接：" prop="downUrl">
          <el-input v-model="ruleForm.downUrl"></el-input>
        </el-form-item>

        <el-form-item label="内容：" prop="content">
          <div style="border: 1px solid #ccc; margin-top: 10px;">
            <!-- 工具栏 -->
            <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" />
            <!-- 编辑器 -->
            <Editor style="height: 400px; overflow-y: hidden;" :defaultConfig="editorConfig" v-model="ruleForm.content" @onChange="onChange" @onCreated="onCreated" />
          </div>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleCancel">取消</el-button>
          <el-button :loading='loading' type="primary" @click="handlePublish">保存</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>

    <el-pagination background layout="prev, pager, next,sizes,total" :total='total' @current-change='current_change' @size-change='size_change' style="text-align: center; margin-top: 60px;" :page-sizes="[10, 50, 100, 1000]">
    </el-pagination>

  </div>
</template>

<script>
import api from '@/api/website/purchasedetails'
import purchaseApi from '@/api/website/purchase'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import '@wangeditor/editor/dist/css/style.css'
import storage, { uploadPath } from '@/api/storage'
import { getToken } from '@/utils/auth'
export default {
  name: 'contact',
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      toolbarConfig: {
        // toolbarKeys: [ /* 显示哪些菜单，如何排序、分组 */ ],
        // excludeKeys: [ /* 隐藏哪些菜单 */ ],
      },
      editorConfig: {
        placeholder: '请输入内容...',
        // autoFocus: false,

        // 所有的菜单配置，都要在 MENU_CONF 属性下
        MENU_CONF: {
          uploadImage: {
            server: uploadPath,
            fieldName: 'file',
            headers: {
              'X-Hyxxkj-Admin-Token': getToken()
            },
            meta: {
              dirName: ''
            },
            onSuccess: () => {},
            onError: (file, err, res) => {
              this.$message.error(`${file.name} 上传出错`)
            }
          },
          uploadVideo: {
            server: uploadPath,
            fieldName: 'file',
            headers: {
              'X-Hyxxkj-Admin-Token': getToken()
            },
            meta: {
              dirName: ''
            },
            onError: (file, err, res) => {
              this.$message.error(`${file.name} 上传出错`)
            }
          }
        }
      },
      isAddOrUpdata: true, // true新增false修改
      createDialogVisible: false,
      rolesList: [],
      total: 0,
      limit: 10,
      page: 1,
      pages: '',
      loading: false,
      purchaseList: '',
      ruleForm: {
        title: '',
        purchaseId: '',
        payPirce: '',
        content: '',
        downUrl: ''
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        payPirce: [{ required: true, message: '请输入价格', trigger: 'blur' }],
        downUrl: [{ required: true, message: '请输入链接', trigger: 'blur' }],
        purchaseId: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ]
      }
    }
  },
  created() {},
  mounted() {
    purchaseApi.list().then((res) => {
      if (res.data.errno == 0) {
        this.purchaseList = res.data.data.list
      }
    })
    this.getList()
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
    },
    onChange(editor) {
      //  console.log('onChange', editor.getHtml()) // onChange 时获取编辑器最新内容
    },
    type(id) {
      var con = ''

      this.purchaseList.map((item) => {
        if (item.id == id) {
          con = item.title
        }
      })

      return con
    },
    handleAddRole() {
      // 新增对话框
      this.isAddOrUpdata = true
      this.createDialogVisible = true
    },
    handlePublish() {
      // 新增图片
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.isAddOrUpdata) {
            this.Create(this.ruleForm)
          } else {
            this.update(this.ruleForm)
          }
        } else {
          this.$message({
            message: '请检查数据必填项',
            type: 'warning'
          })
          return false
        }
      })
    },
    handleCancel() {
      this.createDialogVisible = false
      this.ruleForm.title = ''
      this.ruleForm.purchaseId = ''
      this.ruleForm.payPirce = ''
      this.ruleForm.content = ''
    },
    handleEdit(e) {
      this.isAddOrUpdata = false
      this.ruleForm.title = e.title
      this.ruleForm.purchaseId = e.purchaseId
      this.ruleForm.payPirce = e.payPirce
      this.ruleForm.content = e.content
      this.ruleForm.id = e.id
      this.ruleForm.downUrl = e.downUrl

      this.createDialogVisible = true
    },
    handleDelete(e) {
      api.Delete(e).then((res) => {
        if (res.data.errno == 0) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    current_change(e) {
      this.page = e
      this.getList()
    },
    size_change(e) {
      this.limit = e
      this.getList()
    },
    Create(e) {
      this.loading = true
      api
        .create(e)
        .then((res) => {
          if (res.data.errno == 0) {
            this.loading = false
            this.$message({
              message: '新增成功',
              type: 'success'
            })
            this.getList()
            this.createDialogVisible = false
          }
        })
        .catch((e) => {
          this.loading = false
          this.$message({
            message: e.data,
            type: 'warning'
          })
        })
    },
    update(e) {
      this.loading = true
      api
        .update(e)
        .then((res) => {
          if (res.data.errno == 0) {
            this.loading = false
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.getList()
            this.createDialogVisible = false
          }
        })
        .catch((e) => {
          this.loading = false
          this.$message({
            message: e.data,
            type: 'warning'
          })
        })
    },
    getList() {
      this.loading = true
      api
        .list({
          limit: this.limit,
          page: this.page
        })
        .then((res) => {
          if (res.data.errno == 0) {
            this.rolesList = res.data.data.list
            this.total = res.data.data.list.length
            this.page = res.data.data.page
            this.pages = res.data.data.pages
            this.total = res.data.data.total
            this.limit = res.data.data.limit
            this.loading = false
          }
        })
        .catch(() => {
          this.Loading = false
        })
    }
  }
}
</script>
<style lang="less" scoped>
</style>